package hw850271.lesson1;

/**
 * ���̹߳�����Դ
 * 
 */

public class PrintInteger02 {

	public static void main(String[] args) {
		NumberRange numberRange = new NumberRange(1, 100);
		//NumberPrint np = new NumberPrint(numberRange);
		NumberPrint np = new NumberPrint(numberRange,50);//47ʱ��ֹ��ӡ

		Thread t1 = new Thread(np);
		Thread t2 = new Thread(np);
		Thread t3 = new Thread(np);
		t1.setName("t1");
		t2.setName("t2");
		t3.setName("t3");
		t1.start();
		t2.start();
		t3.start();
	}
}

class NumberPrint implements Runnable {
	private NumberRange numberRange;
	private int interrupt;

	public NumberPrint(NumberRange numberRange) {
		this.numberRange = numberRange;
		this.interrupt = 0;
	}
	
	public NumberPrint(NumberRange numberRange,int interrupt) {
		this.numberRange = numberRange;
		this.interrupt = interrupt;
	}
	
	public void run() {
		int end = numberRange.getEnd();
		while (numberRange.getBegin() < end) {
			synchronized (numberRange) {
				if(numberRange.getBegin() == interrupt+1) {
					Thread.currentThread().interrupt();
					break;
				}
				System.out.println(Thread.currentThread().getName() + ":" + numberRange.getBegin());
				numberRange.setBegin(numberRange.getBegin() + 1);
				try {
					Thread.sleep(50);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
			}
		}
	}
}

class NumberRange {
	private int begin;
	private int end;

	public NumberRange(int begin, int end) {
		this.begin = begin;
		this.end = end;
	}

	public int getBegin() {
		return begin;
	}

	public void setBegin(int begin) {
		this.begin = begin;
	}

	public int getEnd() {
		return end;
	}

	public void setEnd(int end) {
		this.end = end;
	}
}
